/*****************************************************************************************************
*	Copyright Paxs Electronics 2012													
*   All Rights Reserved																
*   The Copyright symbol does not also indicate public availability or publication.	
*
*   Filename:		DataIntegrity.c
*
*   Created by:		Juan Franciso Gomez Ruiz
*   Date Created:	04-20-12
*
*   Description:	Functions to check data integrity.
*
******************************************************************************************************/
	#include "DataIntegrity.h"
/******************************************************************************
*   Local Macro Definitions
******************************************************************************/

/******************************************************************************
*   Local Type Definitions
******************************************************************************/

/******************************************************************************
*   Local Function Declarations
******************************************************************************/

/******************************************************************************
*   Global Variable Definitions
******************************************************************************/

/******************************************************************************
*   Static Variable Definitions
******************************************************************************/

/******************************************************************************
*   Global and Static Function 
******************************************************************************/
	#if CHECKSUM_FORMAT == UNSIGNED_CHAR
	unsigned char VfnCheckSum8(unsigned char * ValidationBuffer, unsigned char BufferSize)
	{
		unsigned short CheckSum=0;
		unsigned char Result;
		if(BufferSize > CHECKSUM_LIMIT)
		{
			BufferSize = CHECKSUM_LIMIT;
		}
		while(BufferSize--)
		{
			CheckSum+=*(ValidationBuffer++);
		}	
		Result = (unsigned char) CheckSum;
		return Result;
	}
	
	unsigned char  VfnDataPackageValidation8(unsigned char PackageCheckSum, unsigned char* ValidationPackage, unsigned char BufferSize)
	{
		// First, calculate the CheckSum from the received Package. 
		unsigned char CalcCheckSum;
		CalcCheckSum = VfnCheckSum8(ValidationPackage,BufferSize);
		//Compare Calculated CheckSum with the Package's CheckSum
		if(PackageCheckSum == CalcCheckSum)
		{
			return 1;		//Return 1, if ChekSums are Equal .
		}
		else
		{
			return 0;		//Return 1, if not.
		}
	}
	#endif
	
	#if CHECKSUM_FORMAT == UNSIGNED_SHORT
/*****************************************************************************
 * Function: 	VfnCheckSum16
 * Description: Calculate a 16bits CheckSum.
 * Parameters:	Validation buffer pointer and buffer size
 * Caveats:		N/A.
 * Return:		16 bits CheckSum.
 *****************************************************************************/		
	unsigned short VfnCheckSum16(unsigned char * ValidationBuffer, unsigned char BufferSize)
	{
		unsigned long CheckSum=0;
		if(BufferSize > CHECKSUM_LIMIT)
		{
			BufferSize = CHECKSUM_LIMIT;
		}
		while(BufferSize)
		{
			CheckSum+=*(ValidationBuffer++);
			BufferSize--;
		}	
		return (unsigned short) CheckSum;
	}
	
/*****************************************************************************
 * Function: 	VfnDataPackageValidation16
 * Description: Validate Package data, by calculating 16bits CheckSum and compare it 
 * 				with the Package's 16bits Checksum. 
 * Parameters:	Package ChekSum received, Data Package received, Package size. 
 * Caveats:		N/A.
 * Return:		0 - CheckSum Validation Fail, 1 - CheckSum Validation OK.
 *****************************************************************************/		
	unsigned char  VfnDataPackageValidation16(unsigned short PackageCheckSum, unsigned char* ValidationPackage, unsigned char BufferSize)
	{
		// First, calculate the CheckSum from the received Package. 
		unsigned short CalcCheckSum;
		CalcCheckSum = VfnCheckSum16(ValidationPackage,BufferSize);
		//Compare Calculated CheckSum with the Package's CheckSum
		if(PackageCheckSum == CalcCheckSum)
		{
			return 1;		//Return 1, if ChekSums are Equal .
		}
		else
		{
			return 0;		//Return 1, if not.
		}
	}
	#endif
